#include "ticksheet.h"

#include <QFile>
#include <QTextStream>

Ticksheet::Ticksheet(QObject* parent, int syncHours, int syncMins, int syncSecs) :
    QObject(parent)
{
    mRecordCount = 0;
    mLastRecordedTime = 0;

    // Set the time and date that this session started at, offset by the sync time
    mStartTime = QTime::currentTime().addSecs( - syncHours*3600 - syncMins*60 - syncSecs);
    mStartDate = QDate::currentDate();

    // Start a new timer to send signals out that the timer changed
    mTimer = new QTimer(this);
    connect(mTimer, SIGNAL(timeout()), this, SLOT(on_timer_timeout()));
    mTimer->start(100);

    // Start the timing
    mElapsedTime = new QTime(mStartTime.hour(), mStartTime.minute(), mStartTime.second());
    mIsRunning = true;
}

Ticksheet::~Ticksheet()
{
    if (mTimer)
        delete mTimer;
}

RecordedTime* Ticksheet::recordTime(const QString& note)
{
    if (!isRunning())
        return 0;

    mRecordCount++;
    qDebug("mRecordCount = %d", mRecordCount);
    int elapsed = mElapsedTime->elapsed();

    RecordedTime* recordedTime = new RecordedTime(mRecordCount, elapsed, note);
    mRecordedTimes.append(recordedTime);
    emit timeRecorded();
    mLastRecordedTime = recordedTime;
    return recordedTime;
}

void Ticksheet::toggleLastTimeWarning()
{
    qDebug("toggleLastTimeWarning");
    if (mLastRecordedTime)
        mLastRecordedTime->toggleWarning();
    emit timeRecorded();
}

bool Ticksheet::isRunning()
{
    return mIsRunning;
}

//
// This stops the timer from updating, and no new times can be added
//
void Ticksheet::stop()
{
    mIsRunning = false;
}

//
// Restarts the timer synced up to the original start time. This is
// useful if the time was stopped by accident or incident
//
void Ticksheet::restart()
{

}

void Ticksheet::saveFile(const QString& fileName)
{
    QFile file(fileName);
    file.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out(&file);
    out << "DATE=" << mStartDate.toString() << "\n";
    out << "TIME=" << mStartTime.toString() << "\n";
    for (int row = 0; row < mRecordedTimes.size(); row++)
    {
        RecordedTime* item = mRecordedTimes.at(row);
        out << item->order() << "\t"
            << item->time() << "\t"
            << item->roundedTime() << "\t"
            << item->note() << "\n";
    }
    file.close();
}

QString Ticksheet::elapsedTimeString()
{
    return mElapsedTime->toString();
}

// Returns a list of recorded times which are flagged shown
QList<RecordedTime*> Ticksheet::shownRecordedTimes()
{
    QList<RecordedTime*> recordedTimes;
    for (int row = 0; row < mRecordedTimes.size(); row++)
    {
        RecordedTime* item = mRecordedTimes.at(row);
        recordedTimes.append(item);
    }
    return recordedTimes;
}

void Ticksheet::on_timer_timeout()
{
    // updateElapsedTime

    if (isRunning())
    {
        int elapsed = mElapsedTime->elapsed();
        emit timeChanged(RecordedTime::calculateTimeString(elapsed));
    }
    else
    {
       emit timeChanged(RecordedTime::calculateTimeString(0));
       if (mTimer)
            mTimer->stop();
    }
}
